Ext.define('BenBen.common.BaseGrid', {
    extend : 'Ext.grid.Panel',
    requires: [
        'BenBen.common.FormWindow'
    ],
    selType: 'checkboxmodel',
	collapsible: false,
    animCollapse: false,
    autoScroll : true,
	height: 480,
	region : 'center',
	margins : '5 5 5 5',
	loadMask : true,
	viewConfig : {
		stripeRows : true,
		enableTextSelection : true
	},
	tbarDefaultItems : 'add,update,look,delete',
	isLoad : true,
	initComponent : function() {
        var me = this;
        var config = this.config;

    	Ext.define('dataModel', {
			extend : 'Ext.data.Model',
			fields : me.modelFields,
			idProperty : 'id'
		});
    	
		var store = new Ext.create('Ext.data.Store', {
			pageSize : config.pageSize,
			model : 'dataModel',
			remoteSort : true,
			proxy: {
		        type: 'rest',
		        actionMethods: {
	                create : 'POST',
	                read   : 'GET',
	                update : 'POST',
	                destroy: 'POST'
	            },
                url : config.url,
		        reader: {
		            type: 'json',
		            root: 'data',
		            totalProperty: 'totalCount'
		        }
		     }
		});
		
		if(me.isLoad){
			store.load({
	            params : {
	                start : 0,
	                limit : config.pageSize
	            }
	        });
		}
		
		var tb  = Ext.create('Ext.toolbar.Toolbar');

        var defalutItems = me.tbarDefaultItems;
        if(defalutItems.indexOf('add') != -1){
            tb.add({
                text : '新增',
                iconCls : 'gridadd',
                handler : Ext.Function.bind(me.addFn, me)
            });
        }

        if(defalutItems.indexOf('update') != -1){
            tb.add({
                text : '修改',
                iconCls : 'gridupdate',
                handler : Ext.Function.bind(me.updateFn, me)
            });
        }

        if(defalutItems.indexOf('look') != -1){
            tb.add({
                text : '查看',
                iconCls : 'gridlook',
                handler : Ext.Function.bind(me.showFn, me)
            });
        }

        if(defalutItems.indexOf('delete') != -1){
            tb.add({
                text : '删除',
                iconCls : 'griddelete',
                handler : Ext.Function.bind(me.deleteFn, me)
            });
        }
        
        if(defalutItems.indexOf('search') != -1){
        	tb.add({
            	xtype: 'searchfield',
                name: 'name',
                store : store,
                hiddenLabel: true,
                width : 200,
                emptyText : '输入查询条件',
                value : ''
            });
        }

        if(me.tbarItems){
            for(var i = 0; i < me.tbarItems.length; i++){
                tb.add(me.tbarItems[i]);
            }
        }

        Ext.apply(this,{
        	columns : me.columns,
        	plugins: me.gridPlugins,
        	store : store,
        	tbar : tb,
    		bbar : Ext.create('Ext.PagingToolbar', {
    			store : store,
    			dock:'bottom',
    			displayInfo : '当前记录 {0} - {1} of {2}',
    			emptyMsg : '暂无记录'
    		})
		});
		this.callParent(arguments);
	},
    createEditWindow : function(type,url,id){
        var me = this;
        var config = this.config;
        if(type == 1){
            me.dataForm.getForm().reset();
        }

        var window = Ext.create('BenBen.common.FormWindow', {
            width : config.winWidth,
            height : config.winHeigth,
            dataForm : me.dataForm,
            config : {
                type : type,
                url : url,
                store : me.getStore(),
                recordId : id
            }
        });

        return window;
    },
	addFn : function() {
        var url = this.config.url;
		this.createEditWindow(1,url,null).show();
	},
	updateFn : function() {
        var record = this.getSeletedRecord();
        if(record != null){
            var url = this.config.url;

            url += '/' + record.get('id');

            var window = this.createEditWindow(2,url,record.get('id'));

            this.loadFormData(url);
            window.show();
		}
	},
	showFn : function() {
        var record = this.getSeletedRecord();
        if(record != null){

            var url = this.config.url;

            url += '/' + record.get('id');

            var window = this.createEditWindow(3 ,url,record.get('id'));

            this.loadFormData(url);
            window.show();
        }
	},
	deleteFn : function() {
		var me = this;
		var record = me.getSeletedRecord();
        if(record != null){
        	var url = me.config.url;

            url += '/' + record.get('id');
    		if(this.getSelectionModel() != null){
    			var record = me.getSelectionModel().getSelection()[0];
    			var store = me.store;
    			Ext.Ajax.request({
                    url: url,
                    method: 'DELETE',
                    callback: function (options, success, response) {
                        if (success) {
                            if (Ext.decode(response.responseText).success) {
                            	Ext.Msg.confirm('成功','删除数据成功');
    						} else {
    							Ext.Msg.confirm('失败',Ext.decode(response.responseText).info);
    						}
                            me.reload();
                        } else {
                            Ext.Msg.confirm('失败','删除数据失败');
                        }
                    }
                });
    		}
        }
	},

    /**
     * 获取grid的store
     */
    getStore : function(){
        return this.store;
    },

    reload : function(){
        this.getStore().load();
    },

    getSeletedRecord : function(){
        if(this.getSelectionModel() != null){
            return this.getSelectionModel().getSelection()[0];
        }

        return null;
    },

    loadFormData : function(url){
    	var me =  this.dataForm;
    	me.getForm().load({
            url : url,
            method:'GET',
            waitMsg : '加载...',
			success : function(form, action) {
				
			}
        });
    }
});